/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aice.chinamapview.component;

import com.aice.chinamapview.ResourceTable;
import com.aice.chinamapview.moudle.MycolorArea;
import com.noteam.chinamapview.ChinaMapView;
import com.noteam.chinamapview.HVScaleScrollView;
import com.noteam.chinamapview.util.ColorChangeUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * CoordinatorBottomLayout
 *
 * @author jjy
 * @since 2021-05-10
 */
public class CoordinatorLayout extends DirectionalLayout {
    private LoadingListener mLoadingListener;
    private ChinaMapView chinaMapView;
    private HVScaleScrollView scrollView;
    private ColorView colorView;
    private HmButton btnChange;
    private int currentColor;
    private HashMap<String, List<MycolorArea>> colorViewHashmap;

    /**
     *
     * 构造函数
     *
     * @param context context对象
     */
    public CoordinatorLayout(Context context) {
        super(context);
        initView();
    }

    /**
     *
     * 构造函数
     *
     * @param context context对象
     * @param attrSet 属性对象
     */
    public CoordinatorLayout(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    /**
     * 设置颜色渐变条
     */
    private void setColorView() {
        colorViewHashmap = new HashMap<>();
        for (int index = 0; index < ColorChangeUtil.getNamestrings().length; index++) {
            String[] colors = ColorChangeUtil.getColorstrings()[index].split(",");
            String[] texts = ColorChangeUtil.getTextstringS()[index].split(",");
            List<MycolorArea> list = new ArrayList<>();
            for (int row = 0; row < colors.length; row++) {
                MycolorArea mycolorArea = new MycolorArea();
                mycolorArea.setColor(Color.getIntColor(colors[row]));
                mycolorArea.setText(texts[row]);
                list.add(mycolorArea);
            }
            colorViewHashmap.put(ColorChangeUtil.getNamestrings()[index], list);
        }
        colorView.setList(colorViewHashmap.get(ColorChangeUtil.getNamestrings()[currentColor]));
    }

    /**
     *
     * 刷新数据
     *
     */
    public void notifyMapColor() {
        btnChange.setText(ColorChangeUtil.getNamestrings()[currentColor]);
        ColorChangeUtil.changeMapColors(
                chinaMapView, chinaMapView.chinaMapModel, ColorChangeUtil.getNamestrings()[currentColor]);
    }

    private void initView() {
        Component layoutScatter = LayoutScatter.getInstance(mContext).
                parse(ResourceTable.Layout_coordinator_layout, this, true);
        btnChange = (HmButton) layoutScatter.findComponentById(ResourceTable.Id_btnChange);
        chinaMapView = (ChinaMapView) layoutScatter.findComponentById(ResourceTable.Id_mapView);
        scrollView = (HVScaleScrollView) layoutScatter.findComponentById(ResourceTable.Id_scrollView);
        scrollView.setChinaMapView(chinaMapView);
        colorView = (ColorView) layoutScatter.findComponentById(ResourceTable.Id_color_view);
        setColorView();
        notifyMapColor();
    }

    /**
     *
     * 设置地图颜色数据
     *
     */
    public void setMapData() {
        btnChange.setText(ColorChangeUtil.getNamestrings()[++currentColor % ColorChangeUtil.getNamestrings().length]);
        if (currentColor > ColorChangeUtil.getNamestrings().length) {
            currentColor = 0;
        } else {
            currentColor = currentColor % ColorChangeUtil.getNamestrings().length;
        }
        notifyMapColor();
        setColorView();
    }

    public void setLoadingListener(LoadingListener listener) {
        mLoadingListener = listener;
    }

    public LoadingListener getLoadingListener() {
        return mLoadingListener;
    }

    /**
     * LoadingListener
     *
     * @author jjy
     * @since 2021-05-10
     */
    public interface LoadingListener {
        /**
         *
         * 刷新
         *
         */
        void onRefresh();
    }

    /**
     *
     * 刷新监听
     *
     */
    public void refresh() {
        if (mLoadingListener != null) {
            mLoadingListener.onRefresh();
        }
    }

    /**
     *
     * 刷新地图颜色
     *
     */
    public void refreshData() {
        colorViewHashmap.clear();
        setMapData();
    }
}
